﻿using System.Collections.Generic;
using System.Text;
using KL.Standard.Domain.MessageQueue;
using KL.Standard.Model.ViewModels.Queue;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;

namespace KL.Standard.RabbitMQ
{
    /// <summary>
    /// 队列生产者积累
    /// </summary>
    public  class BaseQueueProductor<T>:IBaseQueueProductor<T>
    {

        public RabbitMQFactory<T> Factory { set; get; }

        public ILogger<BaseQueueProductor<T>> Logger { set; get; }
        

        public  void PublishMsg<K>(MQueueItem<K> msg, string queueName,string routeKey, string exchangeName = "direct_exchange")
        {
            //创建连接
            using (var connection = Factory.GetConnection())
            {
                //创建信道
                using (var channel = connection.CreateModel())
                {
                    //channel.ConfirmSelect();
                    
                    //创建交换机,Direct
                    channel.ExchangeDeclare(exchangeName, ExchangeType.Direct,durable:true);
                   
                    //channel.QueueDeclare(queueName, true, false, false);
                    //把创建的队列绑定交换机,routingKey不用给值，给了也没意义的
                    channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routeKey);
                    var properties = channel.CreateBasicProperties();
                    properties.Persistent = true; //消息持久化
                    //向交换机写消息
                    string message = msg.ToString();
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchangeName, routingKey: routeKey, properties, body);
                    Logger.LogInformation("发送Direct消息到【{1}】队列:{0}",message,queueName);
                }
            }
        }
        
        
        public  void BroadcastMsg<K>(MQueueItem<K> msg, List<string> queueNames, string exchangeName = "fanout_exchange")
        {
            //创建连接
            using (var connection = Factory.GetConnection())
            {
                //创建信道
                using (var channel = connection.CreateModel())
                {

                    //创建交换机,fanout类型
                    channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout,durable:true);
                    foreach (var queueName in queueNames)
                    {
                        //创建队列
                        //channel.QueueDeclare(queueName, true, false, false);
                        //把创建的队列绑定交换机,routingKey不用给值，给了也没意义的
                        channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: "");
                    }

                    var properties = channel.CreateBasicProperties();
                    properties.Persistent = true; //消息持久化
                    //向交换机写10条消息
                    string message = msg.ToString();
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchangeName, routingKey: "", properties, body);
                    Logger.LogInformation("发送Fanout消息:{0}",message);
                }
            }
        }
    }
}